<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9 0009
 * Time: 15:39
 */
namespace app\admin\controller;
use app\admin\model\AdminUser;
use app\admin\model\UpFile;
use think\Db;
use think\Request;

class Update
{
    /**更新课程分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function classes(Request $request){
        $id =  $request->param('id',null);
        if(!$id){
            return json(['code'=>203,'msg'=>'请输入修改的id']);
        }
        $class_name = $request->param('class_name',null);
        if(!$class_name){
            return json(['code'=>203,'msg'=>'请输入修改的分类名称']);
        }
//        return $id;
        if(Db::name('classes')->where('id',$id)->update(['class_name'=>$class_name,'create_time'=>time()])){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>203,'msg'=>'修改失败']);
        }
    }

    /**更新课程分支
     * @param Request $request
     * @return \think\response\Json
     */
    public function branch(Request $request){
        $data = $request->param();
        $val = val($data,[
            'id'=>'require',
            'tables_class'=>'require',
            'branch_name'=>'require'
        ]);
//        return $data;
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        Db::name('three_level')->where('branchid','in',$data['id'])->update(['classes_id'=>$data['tables_class']]);
        Db::name('bank')->where('branch_id','in',$data['id'])->update(['classes_id'=>$data['tables_class']]);
        if(Db::name('branch')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>205,'msg'=>'修改失败']);
        }
    }

    /**更新题目
     * @param Request $request
     * @return \think\response\Json
     */
    public function question_bank(Request $request){
        $data = $request->param();
        $val = val($data,[
            'id|题目id'        =>'require',
            'stem|题干'        =>'require',
            'answer|答案'      =>'require',
            'right_key|正确答案'=>'require|/[ABCDEFG]/',
            'thinking|做题思路' =>'require',
            'coin|获取的金币'   =>'require',
            'difficulty|题目难度'   =>'require',
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        $bank = Db::name('bank')->where('id',$data['id'])->find();
        $answer = [];
        $answer1 = explode('；',$data['answer']);
        foreach ($answer1 as $value){
            $answer2 = explode(';',$value);
            foreach ($answer2 as $values){
                $answer = array_merge($answer,explode('。',$values));
            }
        }
        $data['answer'] = [];
        foreach ($answer as $value){
            if($value){
                $data['answer'][] = $value;
            }
        }
        if($data['right_key']=='A'){
            $data['right_key']=0;
        }elseif ($data['right_key']=='B'){
            $data['right_key']=1;
        } elseif ($data['right_key']=='C'){
            $data['right_key']=2;
        } elseif ($data['right_key']=='D'){
            $data['right_key']=3;
        }elseif ($data['right_key']=='E'){
            $data['right_key']=4;
        }elseif ($data['right_key']=='F'){
            $data['right_key']=5;
        }elseif ($data['right_key']=='G'){
            $data['right_key']=6;
        }
        $data['create_time'] = time();
        if(UpFile::isFile('picture')){
            $file = $request->file('picture');
            $fileName = UpFile::upload($file);
            if($fileName){
                if($bank['picture']){
                    UpFile::delFile($bank['picture']);
                }
                $data['picture'] = $fileName;
            }
        }
        if(Db::name('bank')->where('id',$data['id'])->json(['answer'])->update($data)){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>205,'msg'=>'修改失败']);
        }
    }

    /**更新职业
     * @param Request $request
     * @return \think\response\Json
     */
    public function occupation(Request $request){
        $data = $request->param();
        $val = val($data,[
            'pid|所属职业' =>'require',
            'occupation_name|职业' =>'require',
            'id'=>'require'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('occupation')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>203,'msg'=>'修改失败,你什么都没有改变']);
        }
    }

    /**更新等级
     * @param Request $request
     * @return \think\response\Json
     */
    public function grade(Request $request){
        $data = $request->param();
        $val = val($data,[
            'id'=>'require',
            'number|等级'=>'require',
            'grade_experience|经验'=>'require'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('grade')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'更新成功']);
        }else{
            return json(['code'=>203,'msg'=>'更新失败,什么都没改变']);
        }
    }

    /**更新第三级分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function three_level(Request $request){
        $data = $request->param();
        $val = val($data,[
            'id'=>'require',
            'three_level_name|第三类名称'=>'require',
            'recommend|是否推荐'=>'require'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('three_level')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'更新成功']);
        }else{
            return json(['code'=>203,'msg'=>'更新失败,没有任何改变']);
        }
    }

    /**更新模式
     * @param Request $request
     * @return \think\response\Json
     */
    public function situation(Request $request){
        $data = $request->param();
//        return $data;
        $val = val($data,[
            'id'=>'require',
            'situation_name|模式名称'=>'require',
            'number|扣除金币'=>'require',
            'completion_icon|模式获取金币'=>'require',
            'completion_exp|模式获取经验'=>'require',
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('situation')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'更新成功']);
        }else{
            return json(['code'=>203,'msg'=>'更新失败,没有任何改变']);
        }
    }

    /**更新提现点
     * @param Request $request
     * @return \think\response\Json
     */
    public function tixiandian(Request $request){
        $id = $request->param('id',null);
        $spot = $request->param('spot',0);
        if(Db::name('three_level')->where('id',$id)->update(['spot'=>$spot])){
            return json(['code'=>200,'msg'=>'更新成功']);
        }else{
            return json(['code'=>203,'msg'=>'更新失败,没有任何改变']);
        }
    }

    /**解答题目
     * @param Request $request
     * @return \think\response\Json
     */
    public function weijietiku(Request $request){
        $data = $request->param();
        $val = val($data,[
            'id|题目id'        =>'require',
            'stem|题干'        =>'require',
            'answer|答案'      =>'require',
            'right_key|正确答案'=>'require|/[ABCDEFG]/',
            'thinking|做题思路' =>'require',
            'coin|获取的金币'   =>'require',
            'difficulty|题目难度'   =>'require',
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        $answer = [];
        $answer1 = explode('；',$data['answer']);
        foreach ($answer1 as $value){
            $answer2 = explode(';',$value);
            foreach ($answer2 as $values){
                $answer = array_merge($answer,explode('。',$values));
            }
        }
        $data['answer'] = [];
        foreach ($answer as $value){
            if($value){
                $data['answer'][] = $value;
            }
        }
        if($data['right_key']=='A'){
            $data['right_key']=0;
            $data['status'] = 1;
        }elseif ($data['right_key']=='B'){
            $data['right_key']=1;
            $data['status'] = 1;
        } elseif ($data['right_key']=='C'){
            $data['right_key']=2;
            $data['status'] = 1;
        } elseif ($data['right_key']=='D'){
            $data['right_key']=3;
            $data['status'] = 1;
        }elseif ($data['right_key']=='E'){
            $data['right_key']=4;
            $data['status'] = 1;
        }elseif ($data['right_key']=='F'){
            $data['right_key']=5;
            $data['status'] = 1;
        }elseif ($data['right_key']=='G'){
            $data['right_key']=6;
            $data['status'] = 1;
        }
        $data['create_time'] = time();
        if(Db::name('bank')->where('id',$data['id'])->json(['answer'])->update($data)){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>205,'msg'=>'修改失败']);
        }
    }

    /**更新配置项
     * @param Request $request
     * @return \think\response\Json
     */
    public function system(Request $request){
        $data = $request->param();
        $val  = val($data,[
            'system_name|设置名称'=>'require',
            'identification|设置标识'=>'require|/[qwertyuiopasdfghjklzxcvbnm]/',
            'system_value|设置值'=>'require',
            'explain|说明'=>'require',
            'id'=>'require'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('system')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>200,'msg'=>'修改失败,你什么都没改变']);
        }
    }

    /**更新后台权限
     * @param Request $request
     * @return string|\think\response\Json
     */
    public function admin_power(Request $request){
        $data = $request->param();
        if(!AdminUser::Auth('all')){
            return '你没有权限';
        }
        $val  = val($data,[
            'power_name|权限名称'=>'require',
            'identification|权限标识'=>'require|/[qwertyuiopasdfghjklzxcvbnm]/',
            'id'=>'require'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(Db::name('admin_power')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>200,'msg'=>'修改失败,你什么都没改变']);
        }
    }

    /**更新后台用户
     * @param Request $request
     * @return string|\think\response\Json
     */
    public function admin_user(Request $request){
        $data = $request->param();
        if(!AdminUser::Auth('all')){
            return '你没有权限';
        }
        $val = val($data,[
            'admin_user|后台用户名'=>'require',
            'admin_power|后台权限'=>'require',
            'id'=>'require'
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        $users = Db::name('admin_user')->where('id',$data['id'])->find();
        if($data['admin_pwd']){
            $data['admin_pwd'] = AdminUser::getPwd($data['admin_pwd']);
        }else{
            unset($data['admin_pwd']);
        }
        if(UpFile::isFile('admin_portrait')){
            $file = $request->file('admin_portrait');
            $fileName = UpFile::upload($file);
            if($fileName){
                if($users['admin_portrait']){
                    UpFile::delFile($users['admin_portrait']);
                }
                $data['admin_portrait'] = $fileName;
            }
        }
        if(Db::name('admin_user')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>200,'msg'=>'修改失败,你什么都没改变']);
        }
    }
}